package com.atguigu.service.impl;

import com.atguigu.base.BaseMapper;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.entity.Permission;
import com.atguigu.helper.PermissionHelper;
import com.atguigu.mapper.PermissionMapper;
import com.atguigu.mapper.RolePermissionMapper;
import com.atguigu.service.PermissionService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl extends BaseServiceImpl<Permission> implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public BaseMapper<Permission> getBaseMapper() {
        return permissionMapper;
    }

    @Override
    public List<Map<String, Object>> findZNodesByRoleId(Long id) {
        List<Map<String,Object>> zNodes = new ArrayList<>();
        //查询所有权限
        List<Permission> allPermissionList = permissionMapper.findAll();
        //已分配的权限Id
        List<Long> assingPermissionIdList = rolePermissionMapper.findPermissionIdListByRoleId(id);

        for (Permission permission : allPermissionList) {

            Map<String, Object> zNode = new HashMap<>();

            if (assingPermissionIdList.contains(permission.getId())) {
                //已分配的权限,分配了就选择
                zNode.put("checked", true);
            } else {
                //未分配的权限
                zNode.put("checked", false);

            }
            zNode.put("id",permission.getId());
            zNode.put("pId",permission.getParentId());
            zNode.put("name", permission.getName());
            zNode.put("open", true);

            zNodes.add(zNode);
        }
        return zNodes;
    }

    @Override
    public List<Permission> findMenusByAdminId(Long adminId) {


        List<Permission> permissionList = permissionMapper.findMenusByAdminId(adminId);
        return PermissionHelper.build(permissionList);
    }

    @Override
    public List<Permission> findAllMenus() {
        List<Permission> permissionList = permissionMapper.findAll();
        return PermissionHelper.build(permissionList);
    }

    @Override
    public List<Permission> findListByAdminId(Long adminId) {
        return permissionMapper.findListByAdminId(adminId);
    }
}
